package com.kinghunt.misp.web.controller.userx;

import com.kinghunt.misp.common.constant.SystemConstant;
import com.kinghunt.misp.common.dto.common.Tree;
import com.kinghunt.misp.common.dto.userx.PermissionDTO;
import com.kinghunt.misp.common.param.userx.EntityPermissionParam;
import com.kinghunt.misp.common.param.userx.PermissionParam;
import com.kinghunt.misp.service.api.userx.PermissionReadService;
import com.kinghunt.misp.service.api.userx.PermissionWriteService;
import com.kinghunt.misp.web.context.ApiContext;
import com.bygoodtech.kings.common.dto.ApiResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * 权限管理
 *
 * @Author jiangtao
 * @Date 2023/7/4 18:45
 */
@RestController
@RequestMapping("/api/userx/permission/")
public class PermissionController {
    @Autowired
    PermissionReadService permissionReadService;
    @Autowired
    PermissionWriteService permissionWriteService;

    /**
     * 获取系统所有权限信息
     *
     * @param appCode 应用code
     * @return 系统权限信息
     */
    @GetMapping("getSystemPermissionTree")
    public ApiResult<Tree<PermissionDTO>> getSystemPermissionTree(String appCode) {
        if (StringUtils.isEmpty(appCode)) {
            appCode = SystemConstant.SYSTEM_CODE;
        }
        return permissionReadService.getSystemPermissionTree(appCode);
    }

    /**
     * 新增系统权限
     *
     * @param permissionParam 新增权限信息
     * @return 是否成功
     */
    @PostMapping(value = "addSystemPermission", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResult<Boolean> addSystemPermission(@RequestBody PermissionParam permissionParam) {
        if (StringUtils.isBlank(permissionParam.getAppCode())) {
            permissionParam.setAppCode(SystemConstant.SYSTEM_CODE);
        }
        return permissionWriteService.addSystemPermission(permissionParam);
    }

    /**
     * 获取角色所有权限信息，包括授权和未授权
     *
     * @param roleId 角色ID
     * @return 角色权限信息
     */
    @GetMapping("getRolePermissionTree")
    public ApiResult<Tree<PermissionDTO>> getRolePermissionTree(Long roleId) {
        return permissionReadService.getRolePermissionTree(roleId);
    }

    /**
     * 获取用户所有权限信息，包括授权和未授权
     *
     * @param userId 用户ID
     * @return 用户权限信息
     */
    @GetMapping("getUserPermissionTree")
    public ApiResult<Tree<PermissionDTO>> getUserPermissionTree(Long userId) {
        return permissionReadService.getUserPermissionTree(userId);
    }

    /**
     * 获取当前登录用户所有权限信息，包括授权和未授权
     *
     * @return
     */
    @GetMapping("getCurrentUserPermissionTree")
    public ApiResult<Tree<PermissionDTO>> getCurrentUserPermissionTree() {
        Long userId = ApiContext.apiUser().getUserId();
        return permissionReadService.getUserPermissionTree(userId);
    }

    /**
     * 更新角色权限
     *
     * @param entityPermissionParam 角色权限
     * @return 是否成功
     */
    @PostMapping(value = "updateRolePermission", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResult<Boolean> updateRolePermission(@RequestBody EntityPermissionParam entityPermissionParam) {
        return permissionWriteService.updateRolePermission(entityPermissionParam.getEntityId(), entityPermissionParam.getPermissionIds());
    }

    /**
     * 更新用户权限
     *
     * @param entityPermissionParam 用户权限
     * @return 是否成功
     */
    @PostMapping(value = "updateUserPermission", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResult<Boolean> updateUserPermission(@RequestBody EntityPermissionParam entityPermissionParam) {
        return permissionWriteService.updateUserPermission(entityPermissionParam.getEntityId(), entityPermissionParam.getPermissionIds());
    }
}
